#include <stdio.h>
#include <stdlib.h>

typedef  int DataType;

#define MAX_SIZE 100
typedef struct {
    DataType data[MAX_SIZE];
    int front;
    int rear;
}Queue;

void initQueue(Queue* queue) {
    queue->front = 0;
    queue->rear = 0;
}

int isQueueEmpty(Queue* queue) {
    return queue->rear == queue->front;

}

int isQueueFull(Queue* queue) {
    return queue->front == (queue->rear+1)%MAX_SIZE;
}
void enQueue(Queue* queue, DataType data) {
    if (isQueueFull(queue)) {
        printf("Queue is full\n");
        return;
    }
    queue->data[queue->rear] = data;
    queue->rear = (queue->rear+1)%MAX_SIZE;   //取模是为了尾指针不出界
}

void showQueue(Queue* queue) {
    if (isQueueEmpty(queue)) {
        printf("Queue is empty\n");
        return;
    }
    int i = queue->front;
    while (i != queue->rear) {
        printf("%d ", queue->data[i]);
        i = (i+1)%MAX_SIZE;
    }
    // for (int i = 0; i < queue->rear; i++) {
    //     printf("%d ", queue->data[i]);
    // }
}

void lengthQueue(Queue* queue) {
    if (isQueueEmpty(queue)) {
        printf("Queue is empty\n");
        return;
    }
    int i = queue->front;
    int j = 0;
    while (i != queue->rear) {
        j+=1;
        i = (i+1)%MAX_SIZE;
    }
    printf("%d\n", j);
}
DataType deleteQueue(Queue* queue) {
    if (isQueueEmpty(queue)) {
        printf("Queue is empty\n");
    }
    DataType temp = queue->data[queue->front];
    queue->front = (queue->front+1)%MAX_SIZE;
    return temp;
}
int main(void) {
    Queue* queue ;
    initQueue(&queue);
    printf("%d\n", isQueueEmpty(&queue));
    enQueue(&queue, 10);
    enQueue(&queue, 20);
    enQueue(&queue, 30);
    showQueue(&queue);
    lengthQueue(&queue);
    deleteQueue(&queue);
    return 0;
}
